package com.guohao.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.guohao.reggie.common.BaseContext;
import com.guohao.reggie.common.R;
import com.guohao.reggie.entity.ShoppingCart;
import com.guohao.reggie.mapper.ShoppingMapper;
import com.guohao.reggie.service.ShoppingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Time;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 购物车管理
 */
@RestController
@RequestMapping("/shoppingCart")
@Slf4j
public class ShoppingController {
    @Autowired
    private ShoppingService shoppingService;

    /**
     * 菜品添加进购物车
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart){

        log.info("shoppingCart : {}",shoppingCart);

        // 1 获取当前登录用户, 未购物车对象赋值
        Long currentId = BaseContext.getCurrentId();
        shoppingCart.setUserId(currentId);

        Long dishId = shoppingCart.getDishId();
        // 2 根据当前登录用户ID 及 本次添加的菜品ID/套餐ID，查询购物车数据是否存在
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,currentId);

        if (dishId != null){
            // 添加购物车是菜品
            queryWrapper.eq(ShoppingCart::getDishId,dishId);
        }else {
            // 添加购物车是套餐
            queryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }

        // 查询购物车数据是否存在
        ShoppingCart cart = shoppingService.getOne(queryWrapper);

        // 3  如果已经存在，就在原来数量基础上加1
        if (cart != null){
            Integer number = cart.getNumber();
            cart.setNumber(number + 1);
            shoppingService.updateById(cart);
        }else {
        // 4 如果不存在，则添加到购物车，数量默认就是1
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingService.save(shoppingCart);
            cart = shoppingCart;
        }

        return R.success(cart);
    }


    /**
     * 查看购物车
     * @return
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> list(){
        log.info("查看购物车...");
        //根据当前登录用户ID查询购物车列表，
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        //进行创建时间的倒序排序。
        queryWrapper.orderByDesc(ShoppingCart::getCreateTime);
        // MP 查询
        List<ShoppingCart> list = shoppingService.list(queryWrapper);

        return R.success(list);
    }

    /**
     * 清空购物车
     * @return
     */
    @DeleteMapping("/clean")
    public R<String> clean(){
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        shoppingService.remove(queryWrapper);
        return R.success("清空购物车成功");
    }

    /**
     * 减少购物车
     * @param shoppingCart
     * @return
     */
    @PostMapping("/sub")
    public R<ShoppingCart> sub(@RequestBody ShoppingCart shoppingCart){
        log.info("购物车减少 : {}",shoppingCart);

        // 1 获取当前登录用户, 未购物车对象赋值
        Long currentId = BaseContext.getCurrentId();
//        shoppingCart.setUserId(currentId);

        Long dishId = shoppingCart.getDishId();
        // 2 根据当前登录用户ID 及 本次添加的菜品ID/套餐ID，查询购物车数据是否存在
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,currentId);

        if (dishId != null){
            // 添加购物车是菜品
            queryWrapper.eq(ShoppingCart::getDishId,dishId);
        }else {
            // 添加购物车是套餐
            queryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }

        // 查询购物车数据是否存在
        ShoppingCart cart = shoppingService.getOne(queryWrapper);
        Integer number = cart.getNumber();
        number --;
        cart.setNumber(number);

        // 3  如果已经存在，就在原来数量基础上加1
        if ( number > 0){
            shoppingService.updateById(cart);
        }else{
            shoppingService.remove(queryWrapper);
        }
        return R.success(cart);
    }

}
